package com.wxtt.android.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.fast.dev.frame.utils.UIUtils;
import com.wxtt.android.R;

/**
 * 说明：空白页面
 *          ---状态（加载中，加载失败，加载成功，网络连接有问题）
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2015/12/31 14:19
 * <p/>
 * 版本：verson 1.0
 */
public class EmptyBridge extends FrameLayout{

    //当前状态
    private int mCurrentState = 0;
    private View.OnClickListener mListener;
    //管理用户的View
    private View mUserView;
    /**********状态********/
    //加载中
    public final static int STATE_LOADING = 1;
    //加载失败
    public final static int STATE_FAIL = 2;
    private static int FAIL_IMAGE = R.mipmap.loading_fail;
    //加载成功
    public final static int STATE_SUCCESS = 3;
    //网络连接错误
    public final static int STATE_NETWORK_ERROR = 4;
    private static int NETWORK_ERROR_IMAGE = R.mipmap.loading_not_network;
    //网络连接超时
    public final static int STATE_NETWORK_TIMEOUT = 5;
    private static int NETWORK_TIMEOUT_IMAGE = R.mipmap.loading_not_network;
    //暂未登录
    public final static int STATE_NO_LOGIN = 6;
    private static int NO_LOGIN_IMAGE = R.mipmap.pagefailed_bg;
    //数据为空
    public final static int STATE_NO_DATA = 7;
    private static int NO_DATA_IMAGE = R.mipmap.pagefailed_bg;
    /**********状态********/

    private View mView;
    private ImageView iv_empty_bridge;
    private TextView tv_empty_bridge;
    private CircleProgressView cpv_empty_bridge;

    public EmptyBridge(Context context) {
        super(context);
        init();
    }

    public EmptyBridge(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public EmptyBridge(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     * 说明：初始化
     */
    private void init(){
        mView = UIUtils.inflate(R.layout.empty_bridge);
        iv_empty_bridge = (ImageView) mView.findViewById(R.id.iv_empty_bridge);
        tv_empty_bridge = (TextView) mView.findViewById(R.id.tv_empty_bridge);
        cpv_empty_bridge = (CircleProgressView) mView.findViewById(R.id.cpv_empty_bridge);
        addView(mView);
        setCurrentState(STATE_SUCCESS);
    }

    /**
     * 说明：设置当前状态
     * @param state
     */
    private void setCurrentState(int state){
        switch (state){
            case STATE_LOADING:
                //加载中
                setCurrentLoadingState();
                mCurrentState = state;
                break;
            case STATE_FAIL:
                //加载失败
                setCurrentFailState();
                mCurrentState = state;
                break;
            case STATE_SUCCESS:
                //加载成功
                setCurrentSuccessState();
                mCurrentState = state;
                break;
            case STATE_NETWORK_ERROR:
                //网络连接错误
                setCurrentNetWorkError();
                mCurrentState = state;
                break;
            case STATE_NETWORK_TIMEOUT:
                //网络连接超时
                setCurrentNetWorkTimeOut();
                mCurrentState = state;
                break;
            case STATE_NO_LOGIN:
                //暂未登录
                setCurrentNoLogin();
                mCurrentState = state;
                break;
            case STATE_NO_DATA:
                //数据为空
                setCurrentNoData();
                mCurrentState = state;
                break;
            default:
                break;
        }
    }

    /**
     * 说明：隐藏空白页面
     * @param flag
     */
    private void setEmptyHide(boolean flag){
        if (flag){
            setVisibility(View.GONE);
            if (mUserView != null){
                mUserView.setVisibility(View.VISIBLE);
            }
        }else {
            setVisibility(View.VISIBLE);
            if (mUserView != null){
                mUserView.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 说明：设置当前状态为---加载中
     */
    private void setCurrentLoadingState(){
        iv_empty_bridge.setVisibility(View.GONE);
        tv_empty_bridge.setVisibility(View.GONE);
        cpv_empty_bridge.setVisibility(View.VISIBLE);
        setEmptyHide(false);
    }

    /**
     * 说明：设置当前状态为---加载失败
     */
    private void setCurrentFailState(){
        iv_empty_bridge.setVisibility(View.VISIBLE);
        tv_empty_bridge.setVisibility(View.VISIBLE);
        cpv_empty_bridge.setVisibility(View.GONE);
        iv_empty_bridge.setImageResource(FAIL_IMAGE);
        tv_empty_bridge.setText(R.string.state_fail);
        setEmptyHide(false);
    }

    /**
     * 说明：设置当前状态为---加载成功
     */
    private void setCurrentSuccessState(){
        iv_empty_bridge.setVisibility(View.GONE);
        tv_empty_bridge.setVisibility(View.GONE);
        cpv_empty_bridge.setVisibility(View.GONE);
        setEmptyHide(true);
    }

    /**
     * 说明：设置当前状态为---网络错误
     */
    private void setCurrentNetWorkError(){
        iv_empty_bridge.setVisibility(View.VISIBLE);
        iv_empty_bridge.setImageResource(NETWORK_ERROR_IMAGE);
        tv_empty_bridge.setVisibility(View.VISIBLE);
        cpv_empty_bridge.setVisibility(View.GONE);
        setEmptyHide(false);
    }

    /**
     * 说明：设置当前状态为---网络超时
     */
    private void setCurrentNetWorkTimeOut(){
        iv_empty_bridge.setVisibility(View.VISIBLE);
        iv_empty_bridge.setImageResource(NETWORK_TIMEOUT_IMAGE);
        tv_empty_bridge.setVisibility(View.VISIBLE);
        tv_empty_bridge.setText(R.string.state_network_timeout);
        cpv_empty_bridge.setVisibility(View.GONE);
        setEmptyHide(false);
    }

    /**
     * 说明：设置当前状态为---未登录
     */
    private void setCurrentNoLogin(){
        iv_empty_bridge.setVisibility(View.VISIBLE);
        iv_empty_bridge.setImageResource(NO_LOGIN_IMAGE);
        tv_empty_bridge.setVisibility(View.VISIBLE);
        tv_empty_bridge.setText(R.string.state_no_login);
        cpv_empty_bridge.setVisibility(View.GONE);
        setEmptyHide(false);
    }

    /**
     * 说明：设置当前状态为---数据为空
     */
    private void setCurrentNoData(){
        iv_empty_bridge.setVisibility(View.VISIBLE);
        iv_empty_bridge.setImageResource(NO_DATA_IMAGE);
        tv_empty_bridge.setVisibility(View.VISIBLE);
        tv_empty_bridge.setText(R.string.state_no_data);
        cpv_empty_bridge.setVisibility(View.GONE);
        setEmptyHide(false);
    }

    private void setImageClick(){
        if (mListener != null){
            findViewById(R.id.ll_emptybridge).setOnClickListener(mListener);
        }
    }

    /***************公开方法*****************/

    /**
     * 说明：设置加载提示
     * @param error
     */
    private void setLoadingText(String error){
        tv_empty_bridge.setText(TextUtils.isEmpty(error)?"":error);
    }

    /**
     * 说明：设置当前状态为---加载中
     */
    public void setLoadingState(){
        setLoadingState("");
    }

    /**
     * 说明：设置当前状态为---加载中
     */
    public void setLoadingState(String msg){
        setLoadingText(msg);
        setCurrentState(STATE_LOADING);
    }

    /**
     * 说明：设置当前状态为---加载失败
     */
    public void setFailState(View.OnClickListener listener){
        setFailState("", listener);
    }

    /**
     * 说明：设置当前状态为---加载失败
     */
    public void setFailState(String msg){
        setFailState(msg, null);
    }

    /**
     * 说明：设置当前状态为---加载失败
     */
    public void setFailState(String msg,View.OnClickListener listener){
        this.mListener = listener;
        setImageClick();
        setLoadingState(msg);
        setCurrentState(STATE_FAIL);
    }

    /**
     * 说明：设置当前状态为---加载成功
     */
    public void setSuccessState(){
        setCurrentState(STATE_SUCCESS);
    }

    /**
     * 说明：设置当前状态为---网络错误
     */
    public void setNetWorkError(View.OnClickListener listener){
        setNetWorkError("", listener);
    }

    /**
     * 说明：设置当前状态为---网络错误
     */
    public void setNetWorkError(String msg){
        setNetWorkError(msg, null);
    }

    /**
     * 说明：设置当前状态为---网络错误
     */
    public void setNetWorkError(String msg,View.OnClickListener listener){
        this.mListener = listener;
        setImageClick();
        setLoadingText(msg);
        setCurrentState(STATE_NETWORK_ERROR);
    }

    /**
     * 说明：设置当前状态为---网络超时
     */
    public void setNetWorkTimeOut(View.OnClickListener listener){
        setNetWorkTimeOut("", listener);
    }

    /**
     * 说明：设置当前状态为---网络超时
     */
    public void setNetWorkTimeOut(String msg){
        setNetWorkTimeOut(msg,null);
    }

    /**
     * 说明：设置当前状态为---网络超时
     */
    public void setNetWorkTimeOut(String msg,View.OnClickListener listener){
        this.mListener = listener;
        setImageClick();
        setLoadingText(msg);
        setCurrentState(STATE_NETWORK_TIMEOUT);
    }

    /**
     * 说明：设置当前状态为---未登录
     */
    public void setNotLogin(View.OnClickListener listener){
        setNotLogin("", listener);
    }

    /**
     * 说明：设置当前状态为---未登录
     */
    public void setNotLogin(String msg){
        setNotLogin(msg, null);
    }

    /**
     * 说明：设置当前状态为---未登录
     */
    public void setNotLogin(String msg,View.OnClickListener listener){
        this.mListener = listener;
        setImageClick();
        setCurrentState(STATE_NO_LOGIN);
    }

    /**
     * 说明：设置当前状态为---暂无数据
     */
    public void setNoData(View.OnClickListener listener){
        setNoData("", listener);
    }

    /**
     * 说明：设置当前状态为---暂无数据
     */
    public void setNoData(String msg){
        setNoData(msg, null);
    }

    /**
     * 说明：设置当前状态为---暂无数据
     */
    public void setNoData(String msg,View.OnClickListener listener){
        this.mListener = listener;
        setImageClick();
        setCurrentState(STATE_NO_DATA);
    }

    /**
     * 说明：设置用户view
     * @param view
     */
    public void setMyView(View view){
        mUserView = view;
    }

    /***************公开方法*****************/

}
